<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发起日期">
        <el-date-picker
          v-model="daterangeUpTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="到账日期">
        <el-date-picker
          v-model="daterangeAccountTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="账户ID" prop="accountId">
        <el-input
          v-model="queryParams.accountId"
          placeholder="请输入账户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账户名称" prop="accountName">
        <el-input
          v-model="queryParams.accountName"
          placeholder="请输入账户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账户归属" prop="accountBelong">
        <el-input
          v-model="queryParams.accountBelong"
          placeholder="请输入账户归属"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="合作媒体" prop="signatory">
        <el-input
          v-model="queryParams.signatory"
          placeholder="请输入合作媒体"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收付款人" prop="recipient">
        <el-input
          v-model="queryParams.recipient"
          placeholder="请输入付款人收款人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="checkType">
        <el-select v-model="queryParams.checkType" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.finance"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="财务台账状态" prop="financeType">
        <el-select v-model="queryParams.financeType" placeholder="请选择财务台账状态" clearable>
          <el-option
            v-for="dict in dict.type.finance"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="6" class="mb8">
      <el-col :span="3">
        <div style="width: 180px;height: 150px;">
          <div style="text-align: center;line-height:50px;width: 100%;height: 50px;color: gray">
            选择合计日期
          </div>
          <div style="text-align: center;line-height:30px;width: 100%;height: 70px">
            <el-date-picker
              style="width: 140px"
              v-model="now"
              align="right"
              type="date"
              value-format="yyyy-MM-dd"
              @change="nowChange"
              placeholder="选择日期"
              :picker-options="pickerOptions">
            </el-date-picker>
          </div>

        </div>
      </el-col>
      <el-col :span="1.5">
        <div style="width: 200px;height: 150px;">
            <div style="text-align: center;line-height:50px;width: 100%;height: 50px;color: gray">
              客户充值合计
            </div>
            <div style="text-align: center;line-height:30px;width: 100%;height: 70px">
              {{mapTotal.cus}}
            </div>
        </div>
      </el-col>

      <el-col :span="1.5">
        <div style="width: 200px;height: 150px;">
          <div style="text-align: center;line-height:50px;width: 100%;height: 50px;color: gray">
            端口充值合计
          </div>
          <div style="text-align: center;line-height:30px;width: 100%;height: 70px">
            {{mapTotal.port}}
          </div>
        </div>
      </el-col>


      <el-col :span="1.5">
        <div style="width: 200px;height: 150px;">
          <div style="text-align: center;line-height:50px;width: 100%;height: 50px;color: gray">
            差值合计
          </div>
          <div style="text-align: center;line-height:30px;width: 100%;height: 70px">
            {{mapTotal.cha}}
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bill:port:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['bill:port:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bill:port:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bill:port:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-connection"
          size="mini"
          @click="matching"
          v-hasPermi="['bill:port:edit']"
        >匹配客户台账</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleRecord"
          v-hasPermi="['bill:port:add']"
        >补录</el-button>
      </el-col>

      <el-col :span="1.5" style="color: gray">
        广告币合计: {{currPort}}
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="portList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="发起日期" align="center" prop="upTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.upTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到账日期" align="center" prop="accountTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.accountTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账户ID" align="center" prop="accountId" />
      <el-table-column label="账户名称" align="center" prop="accountName" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="行业类型" align="center" prop="industryType" />
      <el-table-column label="账户归属" align="center" prop="accountBelong" />
      <el-table-column label="合作媒体" align="center" prop="signatory" />
      <el-table-column label="账户类型" align="center" prop="accountType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.account_type" :value="scope.row.accountType"/>
        </template>
      </el-table-column>
      <el-table-column label="政策类别" align="center" prop="policyType">
      </el-table-column>
      <el-table-column label="充值广告币" align="center" prop="currency" />
      <el-table-column label="端口返点" align="center" prop="portRate" />
      <el-table-column label="应付现金" align="center" prop="inCash" />
      <el-table-column label="实付现金" align="center" prop="actualCash" />
      <el-table-column label="付款人收款人" align="center" prop="recipient" />
      <el-table-column label="剩余备款" align="center" prop="balance" />
      <el-table-column label="对账" align="center" prop="reconciliation">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.finance" :value="scope.row.reconciliation"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="checkType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.finance" :value="scope.row.checkType"/>
        </template>
      </el-table-column>
      <el-table-column label="财务匹配状态" align="center" prop="financeType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.finance" :value="scope.row.financeType"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" width="150px" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bill:port:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bill:port:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改端口台账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row>
          <el-col :span="8">
            <el-form-item label="发起日期" prop="upTime">
              <el-date-picker style="width: 100%" clearable
                              v-model="form.upTime"
                              type="date"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择发起日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="到账日期" prop="accountTime">
              <el-date-picker style="width: 100%" clearable
                              v-model="form.accountTime"
                              type="date"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择到账日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="账户ID" prop="accountId">
              <el-input v-model="form.accountId" placeholder="请输入账户ID" @change="currencyChange"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="账户名称" prop="accountName">
              <el-input v-model="form.accountName" placeholder="请输入账户名称" :disabled="flag"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="公司名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入公司名称" :disabled="flag"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="行业类型" prop="industryType">
              <el-input v-model="form.industryType" placeholder="请输入行业类型" :disabled="flag"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="账户归属" prop="accountBelong">
              <el-input v-model="form.accountBelong" placeholder="请输入账户归属" />
            </el-form-item>
          </el-col>
<!--          <el-col :span="8">-->
<!--            <el-form-item label="账户类型" prop="accountType">-->
<!--              <el-select style="width: 100%" v-model="form.accountType" placeholder="请选择账户类型" @change="portChange">-->
<!--                <el-option-->
<!--                  v-for="dict in dict.type.account_type"-->
<!--                  :key="dict.value"-->
<!--                  :label="dict.label"-->
<!--                  :value="dict.value"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="8">
            <el-form-item label="政策类别" prop="policyType">
              <el-input v-model="form.policyType" placeholder="政策类别" @change="portChange"  :disabled="form.signatory != '快手' && form.signatory != '巨量AD' && form.signatory != '小红书'"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>

          <el-col :span="8">
            <el-form-item label="合作媒体" prop="signatory">
              <el-select style="width: 100%" v-model="form.signatory" placeholder="请选择合作媒体" @change="portChange">
                <el-option
                  v-for="dict in dict.type.medium"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="8">
            <el-form-item label="端口返点" prop="portRate">
              <el-input v-model="form.portRate" placeholder="端口返点" :disabled="form.signatory != '快手广告' &&form.signatory != '快手' && form.signatory != '巨量AD' && form.signatory != '小红书'">
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label-width	="auto" label="充值广告币" prop="currency">
              <el-input style="width: 65%" v-model="form.currency" placeholder="请输入充值广告币" @change="count"/>
            </el-form-item>
          </el-col>

        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="应付现金" prop="inCash">
              <el-input v-model="form.inCash" placeholder="请输入应付现金" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="实付现金" prop="actualCash">
              <el-input v-model="form.actualCash" placeholder="请输入实付现金" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="剩余备款" prop="balance">
              <el-input v-model="form.balance" placeholder="请输入剩余备款" disabled/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label-width	="auto" label="付款人/收款人" prop="recipient">
              <el-input style="width: 65%" v-model="form.recipient" placeholder="请输入付款人/收款人" />
            </el-form-item>

          </el-col>

          <el-col :span="8">
            <el-form-item label="对账" prop="reconciliation">
              <el-input v-model="form.reconciliation" placeholder="请输入对账" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态" prop="checkType">
              <el-select style="width: 100%" v-model="form.checkType" placeholder="请选择状态" disabled>
                <el-option
                  v-for="dict in dict.type.finance"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>

        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">

          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

<!--    补录弹窗-->
    <el-dialog :title="title" :visible.sync="openRecord" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row>
          <el-col :span="8">
            <el-form-item label="发起日期" prop="upTime">
              <el-date-picker style="width: 100%" clearable
                              v-model="form.upTime"
                              type="date"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择发起日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="到账日期" prop="accountTime">
              <el-date-picker style="width: 100%" clearable
                              v-model="form.accountTime"
                              type="date"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择到账日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="账户ID" prop="accountId">
              <el-input v-model="form.accountId" placeholder="请输入账户ID" @change="currencyChange"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="账户名称" prop="accountName">
              <el-input v-model="form.accountName" placeholder="请输入账户名称" :disabled="flag"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="公司名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入公司名称" :disabled="flag"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="行业类型" prop="industryType">
              <el-input v-model="form.industryType" placeholder="请输入行业类型" :disabled="flag"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="账户归属" prop="accountBelong">
              <el-input v-model="form.accountBelong" placeholder="请输入账户归属" :disabled="flag"/>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="8">-->
          <!--            <el-form-item label="账户类型" prop="accountType">-->
          <!--              <el-select style="width: 100%" v-model="form.accountType" placeholder="请选择账户类型" @change="portChange">-->
          <!--                <el-option-->
          <!--                  v-for="dict in dict.type.account_type"-->
          <!--                  :key="dict.value"-->
          <!--                  :label="dict.label"-->
          <!--                  :value="dict.value"-->
          <!--                ></el-option>-->
          <!--              </el-select>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="8">
            <el-form-item label="政策类别" prop="policyType">
              <el-input v-model="form.policyType" placeholder="政策类别" @change="portChange"  :disabled="form.signatory != '快手' && form.signatory != '巨量AD' && form.signatory != '小红书'"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>

          <el-col :span="8">
            <el-form-item label="合作媒体" prop="signatory">
              <el-select style="width: 100%" v-model="form.signatory" placeholder="请选择合作媒体" @change="portChange">
                <el-option
                  v-for="dict in dict.type.medium"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="8">
            <el-form-item label="端口返点" prop="portRate">
              <el-input v-model="form.portRate" placeholder="端口返点" :disabled="form.signatory != '快手广告' &&form.signatory != '快手' && form.signatory != '巨量AD' && form.signatory != '小红书'">
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label-width	="auto" label="充值广告币" prop="currency">
              <el-input style="width: 65%" v-model="form.currency" placeholder="请输入充值广告币" @change="count"/>
            </el-form-item>
          </el-col>

        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="应付现金" prop="inCash">
              <el-input v-model="form.inCash" placeholder="请输入应付现金" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="实付现金" prop="actualCash">
              <el-input v-model="form.actualCash" placeholder="请输入实付现金" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="剩余备款" prop="balance">
              <el-input v-model="form.balance" placeholder="请输入剩余备款" disabled/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label-width	="auto" label="付款人/收款人" prop="recipient">
              <el-input style="width: 65%" v-model="form.recipient" placeholder="请输入付款人/收款人" />
            </el-form-item>

          </el-col>

          <el-col :span="8">
            <el-form-item label="对账" prop="reconciliation">
              <el-input v-model="form.reconciliation" placeholder="请输入对账" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态" prop="checkType">
              <el-select style="width: 100%" v-model="form.checkType" placeholder="请选择状态" disabled>
                <el-option
                  v-for="dict in dict.type.finance"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>

        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitRecordForm">确 定</el-button>
        <el-button @click="cancelRecord">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPort,
  getPort,
  delPort,
  record,
  addPort,
  updatePort,
  accountBelongList,
  getTodayTotal,
  matching
} from "@/api/bill/port";
import { listAccount } from "@/api/baseMain/account";
import { listSupplierPolicy } from "@/api/baseMain/supplierPolicy";
import { getToken } from "@/utils/auth";
export default {
  name: "Port",
  dicts: ['policy_type', 'finance', 'account_type','medium'],
  data() {
    return {
      // 匹配id时间范围
      daterangeUpTime: [],
      // 匹配id时间范围
      daterangeAccountTime: [],
      now:this.getCurrentDate(),
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
      mapTotal:{},
      currPort:0,
      tabPosition: 'left',
      flag:true,
      // 导入参数
      upload: {
        // 是否显示弹出层（导入）
        open: false,
        // 弹出层标题（导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/bill/port/importData"
      },
      // 按钮loading
      buttonLoading: false,
      buttonLoadingRecord: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 端口台账表格数据
      portList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openRecord: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accountId: undefined,
        accountName: undefined,
        companyName: undefined,
        reconciliation: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        accountId: [
          { required: true, message: "账户ID不能为空", trigger: "blur" }
        ],
        currency: [
          { required: true, message: "充值广告币数量不能为空", trigger: "blur" }
        ],
        portRate: [
          { required: true, message: "返点必须有值存在", trigger: "blur" }
        ],
        inCash: [
          { required: true, message: "应付现金必须有值存在", trigger: "blur" }
        ],
        accountBelong: [
          { required: true, message: "账户归属不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    getTodayTotal(this.now).then(response => {
      this.mapTotal.cha = (response.cus - response.port).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
      this.mapTotal.port =  response.port.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
      this.mapTotal.cus = response.cus.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
      this.getList();
    });
  },
  methods: {
    matching(){
      matching().then(res=>{
        this.$modal.msgSuccess("匹配已完成");
        this.getList();
      })
    },
    getCurrentDate(n) {
      var dd = new Date();
      if (n) {
        dd.setDate(dd.getDate() - n);
      }
      var year = dd.getFullYear();
      var month =
        dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
      var day = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
      return year + "-" + month + "-" + day;
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "数据导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('bill/port/importTemplate', {
      }, `port_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    count(){
      if(this.form.portRate && this.form.currency){
        this.form.inCash = (this.form.currency / (Number(1)+Number(this.form.portRate*0.01))).toFixed(2)
      }
    },
    portChange(){
     if(this.form.signatory == '小红书'){
       this.flag = false
     }
      this.form.portRate = undefined
      this.form.inCash = undefined
      if(this.form.accountBelong  && this.form.policyType && this.form.signatory){
        listSupplierPolicy({supplier:this.form.accountBelong,policyType:this.form.policyType,signatory:this.form.signatory}).then( res =>{
          if(res.rows.length >= 1){
            this.form.portRate = res.rows[0].point
            this.count()
          }
        })
      }
    },
    currencyChange(e){
      this.form.accountName = undefined
      this.form.companyName = undefined
      this.form.industryType = undefined
      this.form.accountBelong = undefined
      this.form.portRate = undefined
      this.form.policyType = undefined
      this.form.signatory = undefined
      console.log(this.form)
      listAccount({accountId:e}).then(response =>{
        if(response.rows.length >= 1){
          this.form.accountName = response.rows[0].accountName
          this.form.companyName = response.rows[0].companyName
          this.form.industryType = response.rows[0].industryType
          this.form.accountBelong = response.rows[0].accountBelong
          this.form.policyType = response.rows[0].bidding

          this.form.signatory = response.rows[0].signatory
          this.portChange()

        }else{
          this.flag = false
        }
      })
    },
    nowChange(){
      getTodayTotal(this.now).then(response => {
        this.mapTotal.cha = (response.cus - response.port).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
        this.mapTotal.port =  response.port.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
        this.mapTotal.cus = response.cus.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
        this.getList();
      });
    },
    /** 查询端口台账列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeUpTime && '' != this.daterangeUpTime) {
        this.queryParams.params["beginUpTime"] = this.daterangeUpTime[0];
        this.queryParams.params["endUpTime"] = this.daterangeUpTime[1];
      }
      if (null != this.daterangeAccountTime && '' != this.daterangeAccountTime) {
        this.queryParams.params["beginAccountTime"] = this.daterangeAccountTime[0];
        this.queryParams.params["endAccountTime"] = this.daterangeAccountTime[1];
      }
      listPort(this.queryParams).then(response => {
        this.portList = response.rows;
        this.total = response.total;
        this.currPort = response.currPort !=null? response.currPort.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","):0;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelRecord() {
      this.openRecord = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        upTime: undefined,
        accountTime: undefined,
        accountId: undefined,
        accountName: undefined,
        companyName: undefined,
        industryType: undefined,
        accountBelong: undefined,
        signatory: undefined,
        accountType: undefined,
        policyType: undefined,
        currency: undefined,
        portRate: undefined,
        inCash: undefined,
        actualCash: undefined,
        recipient: undefined,
        balance: undefined,
        reconciliation: undefined,
        checkType: undefined,
        pnsoi: undefined,
        urif: undefined,
        remark: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleRecord(){
      this.reset();
      this.openRecord = true;
      this.title = "补录端口台账";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加端口台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getPort(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改端口台账";
      });
    },
    submitRecordForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoadingRecord = true;
          record(this.form).then(response => {
              this.$modal.msgSuccess("补录成功");
              this.openRecord = false;
              this.getList();
            }).finally(() => {
              this.buttonLoadingRecord = false;
            });
          }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updatePort(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addPort(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      // if(this.ids.length>1){
      //   this.$modal.msgSuccess("暂不支持批量删除");
      //   return;
      // }
      this.$modal.confirm('是否确认删除端口台账编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delPort(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bill/port/export', {
        ...this.queryParams
      }, `port_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
